Visualizing Data With ggplot

1. Install & load the desired packages & data

library("ggplot2")
library("palmerpenguins")

2. View the data that will be visualized

data("penguins")
View(penguins)

*an example of a really cool plot to see

can also add captions to the plot –‘soooo cool’

3. Making a scatterplot with the Penguins dataset plotting flipper length with body mass

 ggplot(data = penguins) +
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g))

Another syntax for the same plot

ggplot(data = penguins, mapping = aes(x = flipper_length_mm, y = body_mass_g)) +
  geom_point()

3. Different aesthetics for the penguins plot

3a.

ggplot(data = penguins) +
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, shape = species))

3b.

ggplot(data = penguins) +
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, shape = species, color = species))

3c.

ggplot(data = penguins) +
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, shape = species, color = species, size = species))

3d.

ggplot(data = penguins) +
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, alpha = species))

3e.

ggplot(data = penguins) +
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g), color = "purple")

3f.

ggplot(data = penguins) +
  geom_smooth(mapping = aes(x = flipper_length_mm, y = body_mass_g))
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

3g.

ggplot(data = penguins) +
  geom_smooth(mapping = aes(x = flipper_length_mm, y = body_mass_g)) +
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g))
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

3h.

ggplot(data = penguins) +
  geom_smooth(mapping = aes(x = flipper_length_mm, y = body_mass_g, linetype = species))
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

3i.

ggplot(data = penguins) +
  geom_jitter(mapping = aes(x = flipper_length_mm, y = body_mass_g))

2. using the facet_grid() to seperate the plots by sex and species subsets

ggplot(data = penguins) +
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, color = species)) +
  facet_grid(sex ~ species)

Loading & viewing the diamonds dataset

1. Visualizing the diamonds dataset using a bar graph with different aesthetics

ggplot(data = diamonds) +
  geom_bar(mapping = aes(x = cut))

1a.

ggplot(data = diamonds) +
  geom_bar(mapping = aes(x = cut, color = cut))

1b.

ggplot(data = diamonds) +
  geom_bar(mapping = aes(x = cut, fill = cut))

1c.

ggplot(data = diamonds) +
  geom_bar(mapping = aes(x = cut, fill = clarity))

1d.

ggplot(data = penguins) +
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, color = species)) +
  facet_wrap(~species)

1e.

ggplot(data = diamonds) +
  geom_bar(mapping = aes(x = color, fill = cut)) +
  facet_wrap(~cut)

Adding labels and annotations to the penguins dataset plots

1.

ggplot(data = penguins) +
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, color = species)) +
  labs(title = "Palmer Penguins: Body Mass vs. Flipper Length")

2.

ggplot(data = penguins) +
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, color = species)) +
  labs(title = "Palmer Penguins: Body Mass vs. Flipper Length", subtitle = "Sample of Three Penguins Species")

3.

ggplot(data = penguins) +
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, color = species)) +
  labs(title = "Palmer Penguins: Body Mass vs. Flipper Length", subtitle = "Sample of Three Penguins Species", caption = "Data collected by Dr. Kristen Gorman")

4.

ggplot(data = penguins) +
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, color = species)) +
  labs(title = "Palmer Penguins: Body Mass vs. Flipper Length", subtitle = "Sample of Three Penguins Species", caption = "Data collected by Dr. Kristen Gorman") + 
  annotate("text",x=220, y=3500, label= "The Gentoos are the largest" )

5.

ggplot(data = penguins) +
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, color = species)) +
  labs(title = "Palmer Penguins: Body Mass vs. Flipper Length", subtitle = "Sample of Three Penguins Species", caption = "Data collected by Dr. Kristen Gorman") + 
  annotate("text",x=220, y=3500, label= "The Gentoos are the largest", color="purple" )

6.

ggplot(data = penguins) +
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, color = species)) +
  labs(title = "Palmer Penguins: Body Mass vs. Flipper Length", subtitle = "Sample of Three Penguins Species", caption = "Data collected by Dr. Kristen Gorman") + 
  annotate("text",x=220, y=3500, label= "The Gentoos are the largest", color="purple", fontface="bold", size=4.5 )

7.

ggplot(data = penguins) +
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, color = species)) +
  labs(title = "Palmer Penguins: Body Mass vs. Flipper Length", subtitle = "Sample of Three Penguins Species", caption = "Data collected by Dr. Kristen Gorman") + 
  annotate("text",x=220, y=3500, label= "The Gentoos are the largest", color="purple", fontface="bold", size=4.5, angle=25 )

8.

p <- ggplot(data = penguins) +
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g, color = species)) +
  labs(title = "Palmer Penguins: Body Mass vs. Flipper Length", subtitle = "Sample of Three Penguins Species", caption = "Data collected by Dr. Kristen Gorman") 

9.

p + annotate("text",x=220, y=3500, label= "The Gentoos are the largest", color="purple", fontface="bold", size=4.5, angle=25 )

Thank you for reading, Ibrahim